package handler

import (
	"github.com/gin-gonic/gin"
	"choujiang/common/models"
	"choujiang/common/utils"
	"choujiang/common/consts"
	"choujiang/box_server/service"
	"fmt"
	"strconv"
)

func GetNewsList(c *gin.Context) {
	form := c.Request.Form
	uniqueId := form.Get("uniqueId")
	user := model.GetUserByUniqueId(uniqueId)
	if user == nil {
		utils.Error(consts.ErrUserNotFound, c)
		return
	}
	news, err := service.GetNewsList(user)
	if err != nil {
		fmt.Printf("GetNewsList err:%+v\n", err)
	}
	utils.JSON(news, c)
}

/**
 @description 获取资讯详情，并增加浏览记录
 @return string
 @author guxiang
 @version v1 2018-11-30
 */
func GetNewsDetail(c *gin.Context) {
	form := c.Request.Form
	uniqueId := form.Get("uniqueId")
	Id := form.Get("id")
	user := model.GetUserByUniqueId(uniqueId)
	if user == nil {
		utils.Error(consts.ErrUserNotFound, c)
		return
	}
	int64Id, _ := strconv.ParseInt(Id, 10, 64)
	news, err := service.GetNewsDetail(int64Id)
	if err != nil {
		fmt.Printf("GetNewsDetail err:%+v\n", err)
	}
	go func() {
		service.ViewNews(user, news)
	}()
	//c.Header("Content-Type", "text/html")
	//c.String(http.StatusOK, news.Content)
	utils.JSON(news, c)
}
